<?php
namespace App\Service\Admin;

class ActionService extends BaseService
{
    //数据列表
    public static function index()
    {
        $res = self::dbConn(self::$dbStAction)
            ->where('pid', 0)
            ->orderBy('sort', 'desc')
            ->get();
        if (!count($res)) {
            return self::toback(-2, '记录不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
            ];
            $childs = self::dbConn(self::$dbStAction)
                ->where('pid', $v->id)
                ->orderBy('sort', 'desc')
                ->get();
            if (count($childs)) {
                foreach ($childs as $vv) {
                    $arys[] = [
                        'id' => $v->id, 'title' => $v->title.'-'.$vv->title,
                    ];
                }
            }
        }
        return self::toback(0, '', $arys);
    }

    //修改
    public static function modify($id, $title)
    {
        $res = self::dbConn(self::$dbStAction)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbStAction)
            ->where('title', $title)
            ->where('id', '<>', $id)
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名数据');
        }
        $res = self::dbConn(self::$dbStAction)
            ->where('id', $id)
            ->update(['title' => $title]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }
}